package com.hyl.job;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.hyl.service.weixin.IOrderService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hyl
 * @description检测未支付回调
 * @create 2025-06-07 11:32
 */
@Slf4j
@Component
public class NopayNotifyOrderJob {
    @Resource
    private IOrderService orderService;
    @Resource
    private AlipayClient  alipayClient;

    @Scheduled(cron = "0/3 * * * * ?")
    public void exet(){
        try{
       /*     log.info("任务：检测未支付收到或未处理的支付回调通知");*/
            List<String> orderIds = orderService.queryNoPayNotifyOrder();
            if (orderIds == null||orderIds.isEmpty())return;

            for (String orderId : orderIds) {
                AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
                AlipayTradeQueryModel bizModel = new AlipayTradeQueryModel();
                bizModel.setOutTradeNo(orderId);
                request.setBizModel(bizModel);

                AlipayTradeQueryResponse alipayTradeQueryResponse = alipayClient.execute(request);
                String code = alipayTradeQueryResponse.getCode();
                // 判断状态码
                if ("10000".equals(code)) {
                    orderService.changeOrderPaySuccess(orderId);
                }
            }

        } catch (Exception e){
            log.error("检测未支付回调失败",e);
        }
    }


}
